package academic.ui.client.widgets;

import ui.client.GenericClickable;
import ui.client.GenericContainerTag;
import academic.ui.client.util.ClientHelper;
import ecommerce.soco.shared.GWT_Product;

import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.DomEvent;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Image;

public class ProductWidget extends Composite {
	
	private GenericContainerTag div = new GenericContainerTag("div");
	
	private GenericClickable buyNow = new GenericClickable("a");
	
	private GenericClickable merchantName = new GenericClickable("a");

	private ClickHandler productFullClicker = new ClickHandler() {

		@Override
		public void onClick(ClickEvent event) {
			NativeEvent e = Document.get().createClickEvent(0, 0, 0, 0, 0, false, false, false, false);
			DomEvent.fireNativeEvent(e, merchantName, merchantName.getElement());
		}
	};
	
	public ProductWidget(){
		div.setStyleName("productWidget");
		
		initWidget(div);
	}
	public void setProduct(GWT_Product product) {	
		GenericContainerTag innerContent = new GenericContainerTag("div");
		innerContent.setStyleName("innerContent");
		div.clear();
		div.getElement().setId(String.valueOf(product.getId()));
		
	
		div.add(new TimerDiv(product.getPurchaseExpiryDate()));
		
		
		GenericContainerTag buyNowDiv = new GenericContainerTag("div");
		buyNowDiv.setStyleName("buyNow");
		buyNowDiv.add(buyNow);
		buyNow.getElement().setId(String.valueOf(product.getId()));
		div.add(buyNowDiv);
		
		div.add(innerContent);
		innerContent.clear();
		merchantName.getElement().setId(String.valueOf(product.getId()));
		merchantName.getElement().setInnerHTML(product.getMerchant().getName());
		merchantName.setStyleName("merchantName");
		GenericContainerTag nameDiv = new GenericContainerTag("div");
		GenericClickable name = new GenericClickable("a",product.getName());
		nameDiv.add(name);
		name.addClickHandler(productFullClicker);
		GenericContainerTag price = new GenericContainerTag("div",ClientHelper.formatPrice(product.getPrice0())+" Buy!");
		GenericContainerTag totValue = new GenericContainerTag("div","Value: "+ClientHelper.formatPrice(product.getPrice1()));
		Image productImage = new Image();
		productImage.addClickHandler(productFullClicker);
		productImage.setUrl(GWT.getModuleBaseURL()+"../PICS/Products/"+product.getImageSmall());
		productImage.setSize("160px", String.valueOf(Math.min(product.getSmallImgRatio()*160,220))+"px");
		productImage.getElement().setId(String.valueOf(product.getId()));
		GenericClickable webAddress = new GenericClickable("a", "View Website");
		webAddress.getElement().setAttribute("href", "http".equals(product.getWebsite().substring(0, 4))?product.getWebsite():"http://"+product.getWebsite());
		webAddress.getElement().setAttribute("target", "_blank");
		GenericContainerTag webAddressDiv = new GenericContainerTag("div");
		webAddressDiv.setStyleName("webAddressDiv");
		webAddressDiv.add(webAddress);
		GenericClickable mapIt = new GenericClickable("a","Map");
		mapIt.getElement().setAttribute("href", "http://mapof.it/"+product.getMerchant().getMapKeywords());
		mapIt.getElement().setAttribute("target", "_blank");
		GenericContainerTag mapItDiv = new GenericContainerTag("div");
		mapItDiv.setStyleName("mapItDiv");
		mapItDiv.add(mapIt);
		GenericContainerTag moreInfoDiv = new GenericContainerTag("div");
		moreInfoDiv.setStyleName("moreInfoDiv");
		GenericClickable moreInfo = new GenericClickable("a","More Info");
		moreInfo.addClickHandler(productFullClicker );
		moreInfoDiv.add(moreInfo);
		
		innerContent.add(merchantName);
		GenericContainerTag productInfoDiv =new GenericContainerTag("div");
		productInfoDiv.add(nameDiv);
		productInfoDiv.add(price);
		productInfoDiv.add(totValue);
		productInfoDiv.add(new GenericContainerTag("div","Discount: "+product.discount()));
		productInfoDiv.setStyleName("paddedDiv");
		innerContent.add(productInfoDiv);
		innerContent.add(productImage);
		innerContent.add(webAddressDiv);
		innerContent.add(mapItDiv);
		innerContent.add(moreInfoDiv);
	}

	
	
	
	public HasClickHandlers buyNow() {
		return buyNow;
	}
	
	public HasClickHandlers productClick() {
		return merchantName;
	}

	
}
